import 'package:flutter/cupertino.dart';
import 'package:jetour_line/aha_jetour_line.dart';
import 'package:jetour_line/base_widget/base_scaffold.dart';
import 'package:jetour_line/base_widget/sliver_list_widget.dart';
import 'package:jetour_line/network/aha_http.dart';
import 'package:jetour_line/pages/baseWidget/bottom_widget.dart';
import 'package:jetour_line/pages/common/commonDetail/common_detail_head_widget.dart';
import 'package:jetour_line/pages/common/commonDetail/common_detail_list_widget.dart';
import 'package:jetour_line/pages/mine/trajectoryDetail/trajectory_detail_head_widget.dart';
import 'package:jetour_line/pages/mine/trajectoryDetail/trajectory_detail_list_widget.dart';
import 'package:jetour_line/utils/extension_int.dart';

import '../../../models/common_detail_model.dart';
import '../../../models/trajectory_detail_model.dart';
import '../../../my_colors.dart';
import 'dart:convert';

class TrajectoryDetailScreen extends StatefulWidget {
  final int id;

  const TrajectoryDetailScreen({Key? key, required this.id}) : super(key: key);

  @override
  State<TrajectoryDetailScreen> createState() => _TrajectoryDetailScreenState();
}

class _TrajectoryDetailScreenState extends State<TrajectoryDetailScreen> {
  TrajectoryDetailModel? detailModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getDetail();
    MapUtils.getMaps();
  }

  @override
  Widget build(BuildContext context) {
    String? dis ;
    if(detailModel != null){
       dis = '总里程${detailModel!.distance}km · 平均时速${detailModel!.speed}km/h · 海拔${detailModel!.asl}m';
      if(detailModel!.distance>1000){
        dis = '总里程${(detailModel!.distance/1000).toStringAsFixed(2)}}km';
      }else if(detailModel!.distance<1){
        dis = '总里程1m';
      }else{
        dis = '总里程${detailModel!.distance}m';
      }
       dis = '$dis · 平均时速${detailModel!.speed} · 海拔${detailModel!.asl}m';
    }

    return BaseScaffold(
        title: '轨迹详情',
        body: detailModel == null
            ? const SizedBox()
            : Column(
                children: [
                  Expanded(
                    child: CustomScrollView(
                      slivers: [
                        TrajectoryDetailHeadWidget(
                          detailModel: detailModel!,
                        ),
                        SliverListWidget.getSliverContainer(
                            100.px,
                            Column(
                              children: [
                                Container(
                                  padding: EdgeInsets.only(left: 24.px),
                                  width: double.infinity,
                                  height: 100.px,
                                  color: MyColors.white,
                                  alignment: Alignment.centerLeft,
                                  child: Text(
                                    "轨迹打卡",
                                    style: TextStyle(
                                        fontSize: 32.px,
                                        color: MyColors.color1A1A1A,
                                        fontWeight: FontWeight.w500),
                                  ),
                                )
                              ],
                            )),
                        TrajectoryDetailListWidget(detailModel: detailModel!)
                      ],
                    ),
                  ),
                  BottomWidget(
                    type: 3,
                    typeId: detailModel!.id,
                    shareTitle: detailModel!.caption,
                    shareSubTitle: dis!,
                    shareImgUrl: '',
                    fav: detailModel!.fav,
                    callBack: (){
                      getDetail();
                    },
                  )
                ],
              ));
  }

  void getDetail() async {
    AhaHttp().get(AhaRouter.MineTrankDetail, params: {"id": widget.id}).then(
        (result) {
          print(json.encode(result));
      //256
      detailModel = TrajectoryDetailModel.fromJson(result);

      setState(() {});
    }).catchError((e) {});
  }
}
